/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.netbuilder.dal.providers.jdbc;

import com.google.code.netbuilder.dal.entities.Wire;
import com.google.code.netbuilder.dal.providers.IWireProvider;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Юрийэ
 */
public class WireProviderJDBC implements IWireProvider{

    @Override
    public void save(Wire entity) throws RuntimeException {
        
        Connection conn = null;
        PreparedStatement pst1 = null;
        ResultSet rs1 = null;
        try{
            
            String queryInsertWires = "Insert into wire(name, creation_date, "
                    + "modification_date, object_state, from_port_id, to_port_id, cable_id) "
                    + "Values(?, ?, ?, ?, ?, ?, NULL)";
            
            pst1 = conn.prepareStatement(queryInsertWires, PreparedStatement.RETURN_GENERATED_KEYS);
            
            pst1.setString(1, entity.getName());
            pst1.setDate(2, new Date(entity.getCreationDate().getTime()));
            pst1.setDate(3, new Date(entity.getModificationDate().getTime()));
            pst1.setInt(4, entity.getObjectState());
            if (entity.getFromPort() != null) 
                pst1.setLong(5, entity.getFromPort().getId());
            else pst1.setNull(5, java.sql.Types.BIGINT);                
            if ( entity.getToPort()!= null) pst1.setLong(6, entity.getToPort().getId());
            else   pst1.setNull(6, java.sql.Types.BIGINT);  

            pst1.executeUpdate();
            rs1 = pst1.getGeneratedKeys();
            rs1.next();
            entity.setId(rs1.getLong(1));
            
            conn.commit();
        } catch (Exception e)
        {
            Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, e);
            try {
                if (conn!= null)
                    conn.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, ex1);
            }
            throw new RuntimeException(e);
        }
        finally {
            try {
                if (rs1!= null) rs1.close();
                if (pst1 != null) pst1.close();
                if (conn != null) {
                    conn.setAutoCommit(true);
                    conn.close();
                }
			} catch (SQLException e) {
                Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, e);
				throw new RuntimeException(e);
            }        
        }
    }

    @Override
    public void update(Wire entity) throws RuntimeException {
        
        Connection conn = null;
        PreparedStatement pst1 = null;
        
        try{
            
            conn = JDBCServise.getConnection();
            conn.setAutoCommit(false);
            String queryUpdateWires = "Update wire set name=?, creation_date=?, "
                    + "modification_date=?, object_state=?, from_port_id=?, to_port_id=? where id=?;";
            
            pst1 = conn.prepareStatement(queryUpdateWires);
            
            pst1.setString(1, entity.getName());
            pst1.setDate(2, new Date(entity.getCreationDate().getTime()));
            pst1.setDate(3, new Date(entity.getModificationDate().getTime()));
            pst1.setInt(4, entity.getObjectState());
            if (entity.getFromPort() != null) 
                pst1.setLong(5, entity.getFromPort().getId());
            else pst1.setNull(5, java.sql.Types.BIGINT);                
            if ( entity.getToPort()!= null) pst1.setLong(6, entity.getToPort().getId());
            else   pst1.setNull(6, java.sql.Types.BIGINT);
            pst1.setLong(7, entity.getId());
            pst1.executeUpdate();               
            conn.commit();
        } catch (Exception e)
        {
            Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, e);
            try {
                if (conn!= null)
                    conn.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, ex1);
            }
            throw new RuntimeException(e);
        }
        finally {
            try {
                if (pst1 != null) pst1.close();
                if (conn != null) {
                    conn.setAutoCommit(true);
                    conn.close();
                }
			} catch (SQLException e) {
                Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, e);
				throw new RuntimeException(e);
            }        
        }
    }

    @Override
    public void delete(Wire entity) throws RuntimeException {
        
        Connection conn = null;
        PreparedStatement pst1 = null;
        PreparedStatement pst2 = null;
        try{
            conn = JDBCServise.getConnection();
            conn.setAutoCommit(false);
            String queryDeleteWireVersion = "Delete from wire_version where wire_id=?;";
            String queryDeleteWires = "Delete from wire where id=?;";
            pst1 = conn.prepareStatement(queryDeleteWireVersion);
            pst2 = conn.prepareStatement(queryDeleteWires);            
                        
            pst1.setLong(1, entity.getId());
            pst2.setLong(1, entity.getId());
            pst1.executeUpdate();
            pst2.executeUpdate();           
                        
            conn.commit();
        } catch(Exception e){
            Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, e);
            try {
                if (conn!= null)
                    conn.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, ex1);
            }
            throw new RuntimeException(e);
        }
        finally{
            try {
                if (pst1 != null) pst1.close();
                if (pst2 != null) pst2.close();
                if (conn != null) {
                    conn.setAutoCommit(true);
                    conn.close();
                }
			} catch (SQLException e) {
                Logger.getLogger(WireProviderJDBC.class.getName()).log(Level.SEVERE, null, e);
				throw new RuntimeException(e);
            }
        }
    }

    @Override
    public List<Wire> findAll() throws RuntimeException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Wire findByID(Integer id) throws RuntimeException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
